package com.users.rn.rncommon.ui;

import android.app.ActionBar;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.users.rn.rncommon.R;
import com.users.rn.rncommon.router.RNSchemeExecute;
import com.users.rn.rncommon.util.ReactConst;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 作者：wolf on 2020/12/6 12:11
 * <p>
 * 邮箱：bluce
 */

public class RNDebugSettingActivity extends AppCompatActivity {
    private EditText pluginNameEdit;
    private EditText componentNameEdit;
    private EditText ipAndPortEdit;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideActionBar();
        setContentView(R.layout.rn_debug_setting_layout);
        pluginNameEdit = findViewById(R.id.debug_plugin_name);
        componentNameEdit = findViewById(R.id.debug_component_name);
        ipAndPortEdit = findViewById(R.id.debug_plugin_ip);
        findViewById(R.id.rn_debug_setting_and_start).setOnClickListener(view -> {
            String pluginName = pluginNameEdit.getText().toString();
            String componentName = componentNameEdit.getText().toString();
            String ipAndPort = ipAndPortEdit.getText().toString();
            if (TextUtils.isEmpty(pluginName)) {
                Toast.makeText(RNDebugSettingActivity.this, "请输入插件名称", Toast.LENGTH_LONG).show();
                return;
            } else if (TextUtils.isEmpty(ipAndPort)) {
                Toast.makeText(RNDebugSettingActivity.this, "请输入IP和端口", Toast.LENGTH_LONG).show();
                return;
            } else if (TextUtils.isEmpty(componentName)) {
                Toast.makeText(RNDebugSettingActivity.this, "请打开页面名称", Toast.LENGTH_LONG).show();
                return;
            } else {
                JSONObject json = new JSONObject();
                try {
                    json.put(ReactConst.DEBUG_STATUS,0);
                    json.put(ReactConst.PLUGINID,pluginName);
                    json.put(ReactConst.DEBUG_HOST,ipAndPort);
                    json.put(ReactConst.MODULEID,"index");
                    json.put(ReactConst.COMPONENTNAME,pluginName);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                RNSchemeExecute.getInstance().openPage(RNDebugSettingActivity.this, json.toString());
            }
        });
    }

    private void hideActionBar() {
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
    }


}
